/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dvsdk.util;

import dvsdk.GlobalConfig;
import java.nio.charset.Charset;
import org.apache.commons.codec.binary.Base64;

/**
 *
 * @author Develop
 */
public class UtilSec {
    /**
     * String to Base64
     *//*
    @Deprecated
    public static String base64Encode(String input) {
    sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
    return encoder.encode(input.getBytes());
    }*/
    
    /**
     * String (UTF-8) to Base64
     */
    public static String base64Encode(String input, String encoding) {
        try {
            String ret = new String(input.getBytes(Charset.defaultCharset()), encoding);
            return base64Encode(ret); 
        }
        catch (Exception ex) {
            return input;
        }
    }
    /**
     * String (UTF-8) to Base64
     */
    public static String base64Encode(String input) {
        try {
            Base64 encoder = new Base64();
            byte[] result = encoder.encode(input.getBytes("UTF-8"));
            return new String(result, "UTF-8");
        } catch (Exception ex) {
            if (GlobalConfig.DEBUG > 0) {
                System.err.println(ex.getLocalizedMessage());
            }
            if (GlobalConfig.DEBUG > 1) {
                ex.printStackTrace();
            }
            return "";
        }
    }
}
